<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link  th:href="@{/lib/layui/css/layui.css}" href="../../static/lib/layui/css/layui.css" rel="stylesheet">
    <link type="text/css" th:href="@{/css/login.css}" rel="styleSheet" href="../../static/css/login.css"/>
</head>
<body>
<div id="login" class="login-class">
    <form class="layui-form" action="">
        <div class="login-content">
            <div class="login-title">
                <img class="login--title-img" th:src="@{/images/login-title-icon.png}" src="../../static/images/login-title-icon.png" alt="">
                &nbsp;&nbsp;&nbsp;
                <div class="login-title-text">挚夕影院</div>
            </div>
            <!-- tabs切换开始 -->
            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                <ul class="layui-tab-title div-tab-title">
                    <li class="layui-this">登录</li>
                    <li>注册</li>
                </ul>
                <div class="layui-tab-content" data-index="0">
                    <!-- 登录 -->
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <i class="layui-icon layui-icon-username"></i>
                                <input type="text" name="account" id="account" placeholder="用户名"
                                       lay-verify="accountverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <i class="layui-icon layui-icon-password"></i>
                                <input type="password" name="password" id="password" placeholder="密码"
                                       lay-verify="passverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <!-- 注册 -->
                    <div class="layui-tab-item">
                        <div class="layui-form-item">
                            <div class="layui-input-inline">
                                <i class="layui-icon layui-icon-cellphone"></i>
                                <input type="text" name="email" id="email" placeholder="邮箱"
                                       lay-verify="emailverify" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-inline-code">
                                <div class="layui-input-inline-code-input">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                    <input type="text" name="code" id="code" style="width:150px;"
                                           placeholder="验证码" autocomplete="off" class="layui-input">
                                </div>
                                <div class="login-code-btn">
                                    <button type="button" class="layui-btn layui-btn-primary">获取验证码</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
            <!-- tabs切换结束 -->
            <div class="tip"><a href="findPassword.html" class="save-tip">忘记密码</a></div>
            <!-- 登录按钮 -->
            <div class="foot-form-item">
                <button class="layui-btn layui-btn-normal login-btn" lay-submit lay-filter="login">登录</button>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/lib/layui/layui.js}" src="../../static/lib/layui/layui.js"></script><!-- 引入 layui.js -->
<script>
    layui.use(['form', 'element', 'layer', 'jquery'], function () {
        var element = layui.element;
        var form = layui.form;
        var $ = layui.jquery;
        $(function () {
            // 立即触发

        })
        //一些事件
        element.on('tab(docDemoTabBrief)', function (data) {
            // console.log(this)
            // console.log(data);
            // console.log(data.index); //得到当前Tab的所在下标
            // console.log(data.elem); //得到当前的Tab大容器
            if (data.index == 0) {
                $('#email').val('')
                $('#code').val('')
                $(".layui-tab-content").attr('data-index', 0)
            } else {
                $('#account').val('')
                $('#password').val('')
                $(".layui-tab-content").attr('data-index', 1)

            }
        });
        form.verify({
            //用户名
            accountverify: function (value, item) { //value：表单的值、item：表单的DOM对象
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '用户名不能为空';
                    }
                    if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                        return '用户名不能有特殊字符';
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                }
            }

            //密码
            , passverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '密码不能为空';
                    }
                    if (!/^\w+$/.test(value)) {
                        return '只能输入数字和字母'
                    }
                    if (!/^[A-Za-z0-9]{6,16}$/.test(value)) {
                        return '密码必须6到16位，且不能出现空格'
                    }
                }
            },
            //邮箱
            emailverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '邮箱不能为空';
                    }
                    if (!/^1[3-9]\d{9}$/.test(value)) {
                        return '请输入正确的邮箱';
                    }
                }
            },
            //验证码
            codeverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '验证码不能为空';
                    }
                }
            }


        });
        //提交
        form.on('submit(login)', function (data) {
            // console.log(data)
            // let dataform = data.field;
            // let Typeindex = $(".layui-tab-content").attr('data-index')
            // console.log(Typeindex)
            // layer.msg(JSON.stringify(data.field));
            $.ajax({
                url: '[[@{/login.do}]]',
                type: 'post',
                data: {
                    username: $("#account").val(),
                    password: $("#password").val()
                },
                success: function (response) {
                    if (response.status == 'success') {
                        //跳转到首页
                        window.location.href = "[[@{/}]]"
                        layer.msg(JSON.stringify(response.data))
                    } else {
                        //登录失败
                        layer.msg(JSON.stringify(response.data))
                    }
                },
                error: function (response) {
                    layer.msg(JSON.stringify(response.responseText));
                }
            })
            return false;
        });
    });

</script>
</body>
</html>